#!/usr/bin/python
# -*- coding: UTF-8 -*-

"""
@author: jiu
@contact: 643562684@qq.com
@time: 2022/4/4 
"""
from time import sleep

from browser.chrome import CHROME
from common.read_Yaml import Read_Yaml
from common.read_Ini import Read_InI
from Conf.setting import YAML_FILE_LIST, PROJECT_URL_MANAGER_PAGE, PROJECT_CONF_SETTING
from PageProjects.common_page import CommonPage


class BackstageReviewLoan(CommonPage):
    # 数据页加载
    CommonPage.element_data.update(
        Read_Yaml(YAML_FILE_LIST["bg"]).data
    )

    _user_info = Read_InI(PROJECT_CONF_SETTING['project']).data['manage']
    #  替换url
    _url = PROJECT_URL_MANAGER_PAGE

    _text = None

    def login_manage(self):
        """后台登入"""
        self.open_url(self._url)
        self.element_input(self.manage_login_user, value=self._user_info["username"], page_action="后台登入输入用户名")
        self.element_input(self.manage_login_pwd, value=self._user_info["password"], page_action="后台登入输入密码")
        self.element_input(self.manage_login_verify_code, value=self._user_info["verify_code"], page_action="后台登入输入验证码")
        self.el_click(self.manage_login_btn, page_action="点击后台登入按钮")
        sleep(3)

    def execution_process_pe(self, obj_name: str):
        """初审执行流程"""
        # 替换占位符为审核对象
        self.agency_affairs_audit_object[1] = self.agency_affairs_audit_object[1].replace("###", obj_name)
        self.el_click(self.agency_affairs_audit_object, page_action=f"点击初审初对象: {obj_name}")
        self.element_input(self.agency_affairs_sub_name, value=self._dt.random_text[:5], page_action="定位简短名称进行输入")
        self.select_el(self.agency_affairs_classification, get_type='index', el_value=self._dt._random_number(1, 7),
                       page_action="定位分类下拉框,选择元素的方式为[index]")
        self.select_el(self.agency_affairs_contract, get_type='index', el_value=self._dt._random_number(1, 10),
                       page_action="定位借款合同范本下拉框,选择元素的方式为[index]")
        self.select_el(self.agency_affairs_tcontract_id, get_type='index', el_value=self._dt._random_number(1, 10),
                       page_action="定位转让合同范本下拉框,选择元素的方位[index]")
        self.el_click(self.agency_affairs_deal_status, page_action="点击审核成功")
        self.el_click(self.agency_affairs_data_box, page_action="点击选择时间按钮")
        self.el_click(self.agency_affairs_data, page_action="选择日期控件中今天的日期")
        self.el_click(self.agency_affairs_edit, page_action="点击编辑按钮")

    def execution_process_ta(self, obj_name: str):
        """复审审核流程"""

        # 替换占位符为审核对象
        self.to_review_obj[1] = self.to_review_obj[1].replace("###", obj_name)
        self.el_click(self.to_review_obj, page_action=f"点击复审对象: {self.to_review_obj[1]}")
        self.el_click(self.to_review_success, page_action="复审审核通过")
        self.el_click(self.agency_affairs_edit, page_action="复审中点击编辑按钮")

    def preliminary_examination(self, to_examine=None):
        """初审"""
        self.switch_frame(self.agency_affairs_frame, page_action="切换到操作的frame中")
        self.el_click(self.agency_affairs_preliminary_examination, timeout=25, page_action="点击带初审的连接")

        if to_examine:  # 全部初审
            for examine_obj in self.audit_materials[::-1]:
                self.execution_process_pe(examine_obj)

        else:  # 单个初审
            _obj = self.audit_materials[-1]
            self.execution_process_pe(_obj)
            # 单个初审刷新页面进入复审
            self.browser_flash(page_action="刷新网页")
        # self._text = self.get_text(self.preliminary_examination_assert, page_action="获取初审执行结果")

    def to_review(self, tr=None):
        """复审"""
        self.switch_frame(self.agency_affairs_frame, page_action="切换到操作的frame中")
        self.el_click(self.agency_affairs_to_review, timeout=25, page_action="点击复审连接")
        if tr:
            for examine_obj in self.audit_materials[::-1]:
                self.execution_process_ta(examine_obj)
        else:
            _obj = self.audit_materials[-1]  # 最后的一个审核对象
            self.execution_process_ta(_obj)
            # self.browser_flash(page_action="刷新网页")
        # self._text = self.get_text(self.preliminary_examination_assert, page_action="获取初审执行结果")
